'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "";
            $scope.imgData={};
            $scope.url="https://ifci.lincomb.com";//本地也显示图片
            detail();
            $scope.title = "基本配置";
            $scope.loading = true;
            //初始化验证
            $scope.submit= function(){
                $scope.loading = true;
                var picUrl = $scope.imgData[0];
                if (!picUrl) {
                    $scope.alertModal({'content':'请上传轮播图片'});
                    return;
                }
                var picUrl = $scope.imgData[0].src;
                $scope.record.value = picUrl[0];
                saveData();
            };

            function saveData(){
                var m = $scope.record;
                console.log(m);
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url : $scope.app.project_name + '/notice/updateImg',
                        data: m
                    }).then(callback);
                }
                function callback(result){
                    if(result.httpCode ==200){
                        $scope.alertModal({"content":"操作成功"});
                        $timeout(function(){
                            $state.go('main.headlines.notice.basic');
                        },2000);
                    }else{
                        $scope.alertModal({"content":"操作失败"});
                        $scope.isDisabled = false;
                    }
                    $scope.loading = false;
                }
            }
            //开关
            $scope.oper= function($event) {
                $.ajax({
                    url : $scope.app.project_name+'/notice/enableOrdisabled',
                    data: {value:$event.target.checked?'Y':'N'}
                }).then(function(result) {
                    $scope.loading = false;
                    $scope.alertModal({"content":"操作成功"});
                    if (result.httpCode != 200) {
                        $scope.alertModal(result.msg);
                    }
                    $scope.search();
                });
            }
            // 初始化页面
            function detail() {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/notice/noticeService',
                    data: {'id': 'id'}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                        $scope.imgData = [];
                        var srcArr = [];
                        srcArr.push($scope.record.noticeIco);
                        $scope.imgData.push({
                            src:srcArr
                        })
                        console.log($scope.imgData)
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });

            }


        }]);